package com.neusoft.shopping.manager.controller;


import com.neusoft.shopping.manager.comm.Page;
import com.neusoft.shopping.manager.entity.GoodsEntity;
import com.neusoft.shopping.manager.entity.PictureEntity;
import com.neusoft.shopping.manager.service.GoodsService;
import com.neusoft.shopping.manager.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class GoodsController {
    @Autowired
    GoodsService service;
    @Autowired
    PictureService pictureService;
    @RequestMapping("/queryGooByPage/{page}")
    public  ModelAndView getGoodsBypage(@PathVariable("page") Integer pageid){
        Page<GoodsEntity> page=new Page<>();
        List<PictureEntity> pictureList = new ArrayList<>();
        ModelAndView modelAndView=new ModelAndView();
        if(null!=pageid||pageid.intValue()!=0){
            page.setCurenPage(pageid);
        }
        page=service.queryAll(page);
        page.getArrys().forEach(goodsEntity -> {
               PictureEntity pictureEntity = pictureService.qureyBysGoodsno(goodsEntity.getGoodsno());
               pictureList.add(pictureEntity);
        });
        modelAndView.addObject("pictures",pictureList);
        modelAndView.addObject("page",page);
        modelAndView.setViewName("goods_page");
        return modelAndView;
    }

    @RequestMapping("/queryGooByOne/{goodsno}")
    public ModelAndView GetGoodsByOne(@PathVariable("goodsno") Integer goodsno){
        ModelAndView modelAndView=new ModelAndView();
        Page<GoodsEntity> page=new Page<>();
        GoodsEntity goods=service.getGoodsByOne(goodsno);
        List<GoodsEntity> list=new ArrayList<GoodsEntity>();
        list.add(goods);
        page.setArrys(list);
        modelAndView.addObject("page",page);
        modelAndView.setViewName("goods_page");
        return modelAndView;
    }

    @RequestMapping("/goodsModify/{method}/{goodsno}")
    public ModelAndView GoodsModify(@PathVariable("method") String method,@PathVariable("goodsno") Integer goodsno){
        ModelAndView modelAndView=new ModelAndView();
        if(method.equals("update")){
            GoodsEntity goods=service.getGoodsByOne(goodsno);
            PictureEntity pictureEntity = pictureService.qureyBysGoodsno(goods.getGoodsno());
            modelAndView.addObject("goods",goods);
            modelAndView.addObject("picture",pictureEntity);
            modelAndView.setViewName("goods_modify");
            return modelAndView;
        }
        else if (method.equals("add")){
            modelAndView.setViewName("goods_modify");
            return modelAndView;
        }else {
            service.del_goods(goodsno);
            return this.getGoodsBypage(1);
        }
    }

    @RequestMapping(value = "/goodsModifyConserve")
    public String  GoodsModifyConserve(@RequestParam("method") String method , GoodsEntity goods,
     @RequestParam("file") MultipartFile multipartFile){
        if(method.equals("add")){
            service.add_goods(goods);
            Integer goodsno=service.querylast().getGoodsno();
            pictureService.dofile(multipartFile,goodsno);
        }
        if(method.equals("update")){
            service.update_goods(goods);
                System.out.println(multipartFile.getOriginalFilename());
                Integer goodsno=service.querylast().getGoodsno();
               pictureService.dofile(multipartFile,goodsno);
        }
        return "forward:queryGooByPage/1";
    }
}
